using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class Recruiters_responseletter : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["rcuserid"] == null)
            Response.Redirect("login.aspx");
        connectionString =
         WebConfigurationManager.ConnectionStrings[
           "RecruitVietnam"].ConnectionString;
        
        Page searchPage = this.PreviousPage;
        if(!IsPostBack)
        {
            string responseID = Request.QueryString.Get("responseID");
            if(responseID !=null && responseID != "")
            {
                btnSave.Text = "Update";
                lblResponseID.Text = responseID;
                EditLetter(responseID);
             }
             else
                 btnSave.Text = "Save";
        }
             
        
    }
     
    void EditLetter(string letterID)
    {
        try
        {
            SqlServerProvider cls = new SqlServerProvider();
            cls.ConnectionString = connectionString;

            cls.CommandText = "select * from "+
            "tblResponseLetters where RecruiterID="
             + Session["rcuserid"].ToString() + 
             " and ResponseID='" + letterID +"'";
            DataRow dataRow = cls.GetDataRow();
            if(dataRow != null)
            {
                txtSubject.Text = 
                dataRow["ResponseSubject"].ToString();
                txtBody.Text = 
                dataRow["ResponseBody"].ToString();
                txtSignature.Text = 
                dataRow["ResponseSignature"].ToString();
            }
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
     

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
            {

                if (txtSubject.Text != "")
                {
                    RecruitVietnam rcv
                                = new RecruitVietnam();
                    rcv.ConnectionString =
                    connectionString;
                    if (!rcv.SaveResponse(Session["rcuserid"].ToString(),
                    lblResponseID.Text, txtSubject.Text, 
                    txtBody.Text, txtSignature.Text) )                    
                        litError.Text = rcv.ErrorMessage;
                    else
                        Response.Redirect("responseletters.aspx");
                }
                    
            }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
}
